<?php

/*
  Project: Blogmer - Open Source Blogging Software
  Type: PHP and database
  Desc: Contains all functions related to categories.
  Author: Yashpal
  Usage:  	$this->load->model('Users_model','',TRUE);	-Load model
			$this->Users_model->UserRegistration();  	- Insert value in database
			$this->Users_model->ForgetPassword();    	- Generate reset password url
			$this->Users_model->ResetPassword();     	- Generate new password
			self::randomstrings(number) 				- Local use only
			

  Function name : 
  UserRegistration       -> This function will insert user detail in database.
  ForgetPassword()       -> This function takes email as input and generate an reset password url.  
  ResetPassword()		 -> This function will reset the password and generate a new password.	
  randomstrings($number) -> This function will create random alphanumeric given number of string.
  
  MemberGroups

      Admin                              5
      Author/Contributer                 34
      Member                             69
      Guest                              106
      Ban                                124

*/

class Posts_model extends Model {

    function Posts_model()
    {
        // Call the Model constructor
        parent::Model();
    }
	
	function create_blog()
	{
		 $ccat=$this->input->post('ccat');
		 $blogcats= implode(",",$ccat);
		 //produce something like this   2008-08-15 06:16PM
		 $time=$this->input->post('blog_time');	 
		 $time=explode(':',$time);	 
		 $time[0]=str_pad($time[0],2,'0',STR_PAD_LEFT);
		 $finaltime=$time[0].":".substr($time[1],0,2).":".rand(10,59);
		 $date=$this->input->post('blog_date')." ".$finaltime;
			 
         $insert = array(
                'PostID' => '',
				'Title' => $this->input->post('Title'),
				'URLTitle' => url_title($this->input->post('Title'),'dash'),
				'Label' => $this->input->post('Title'),
                'Link' => $this->input->post('Link'),
                'Content' => $this->input->post('Content'),
				'Author' => $this->input->post('Author'),
				'AuthorEmail' => $this->input->post('AuthorEmail'),
				'CatID' => $blogcats,
				'Meta' => $this->input->post('Meta'),
				'Keyword' => $this->input->post('Keyword'),
				'Description' => $this->input->post('Description'),
				'CommentNotification' => $this->input->post('CommentNotification'),
				'PasswordProtected' => $this->input->post('PasswordProtected'),
				'Comments' => $this->input->post('Comments'),
				'TotalComments' => 0,
				'Pings' => $this->input->post('Pings'),
				'TagClouds' => $this->input->post('TagClouds'),
				'PostedDate' =>$date,
				'Summary' => $this->input->post('Summary'),
				'Status' => $this->input->post('Status')
                );
           
		   $check=$this->db->insert('posts', $insert);
			
			if($check)
			{
				//Post successfully posted.
				$data['title']="Information Page";
				$data['header']="Information";
				$data['url']='/admin/ManageBlogs/';
				$data['time']="2";
				$data['info']="Post is successfully created";
				$this->load->view('admin/info',$data);
		
	 
		 	}
	}
	
	function edit_blog($post_id,$page_id)
	{
			 $page_id=self::RefinePagNav($page_id);
			 $ccat=$this->input->post('ccat');
			 $blogcats= implode(",",$ccat);
			 $time=$this->input->post('blog_time');	 
			 $time=explode(':',$time);	 
			 $time[0]=str_pad($time[0],2,'0',STR_PAD_LEFT);
			 $finaltime=$time[0].":".substr($time[1],0,2).":".rand(10,59);
			 $date=$this->input->post('blog_date')." ".$finaltime;
		 
             $insert = array(
                
				'Title' => $this->input->post('Title'),
				'URLTitle' => url_title($this->input->post('Title'),'dash'),
				'Label' => $this->input->post('Title'),
                'Link' => $this->input->post('Link'),
                'Content' => $this->input->post('Content'),
				'Author' => $this->input->post('Author'),
				'AuthorEmail' => $this->input->post('AuthorEmail'),
				'CatID' => $blogcats,
				'Meta' => $this->input->post('Meta'),
				'Keyword' => $this->input->post('Keyword'),
				'Description' => $this->input->post('Description'),
				'CommentNotification' => $this->input->post('CommentNotification'),
				'PasswordProtected' => $this->input->post('PasswordProtected'),
				'Comments' => $this->input->post('Comments'),
				'TagClouds' => $this->input->post('TagClouds'),
				'PostedDate' =>$date,
				'Summary' => $this->input->post('Summary'),
				'Status' => $this->input->post('Status')
                );
           $this->db->where(array('PostID'=>$post_id));
		   $check=$this->db->update('posts', $insert);
			
			if($check)
			{
				//Post successfully posted.
				$data['title']="Information Page";
				$data['header']="Information";
				$data['url']='/admin/ManageBlogs/'.$page_id;
				$data['time']="2";
				$data['info']="Post is successfully updated";
				$this->load->view('admin/info',$data);
		
		 	}
	}
	
	
	function delete_post($post_id,$page_id,$post_title)
	{

		$page_id=self::RefinePagNav($page_id);
		$this->db->where('PostID', $post_id);
		$result=$this->db->delete('posts');
		
		if($result)
			$data['info']="<p>Post Deleted Succesfully</p>";
		else	
			$data['info']="<p>Post Deletion Failed</p>";
		
		$this->db->where('URLTitle', $post_title);
		$result=$this->db->delete('comments');
		
		if($result)
			$data['info'].="<p>Related comments Deleted Succesfully</p>";
		else	
			$data['info'].="<p>Related comments Deletion Failed</p>";
		
			$data['title']="Information Page";
			$data['header']="Information";
			$data['url']='/admin/ManageBlogs/'.$page_id;
			$data['time']="2";
			
			$this->load->view('admin/info',$data);
		
		//redirect(, 'refresh');
	}

 	function list_posts($pageid)
	{
		$pageid=self::RefinePagNav($pageid);
		$this->db->from('posts');
	 	$cnt=$this->db->count_all_results();
		
		$config['base_url'] = site_url().'/admin/ManageBlogs/';
		$config['total_rows'] =$cnt;
		$config['per_page'] = '10';
		$config['num_links'] = '3';
		$config['uri_segment'] = '3';


		$this->pagination->initialize($config); 
		$this->db->order_by("PostedDate", "desc"); 
		$query = $this->db->get('posts',$config['per_page'],$this->uri->segment(3));
		
		$out= "<table width=\"100%\" border=\"0\">";
		$out.="<caption>Posts List</caption>
			<thead>
				<tr>
					<th scope=\"col\">Posted Date</th>
					<th scope=\"col\">Post Title</th>

					<th scope=\"col\">Status</th>
					<th scope=\"col\">View</th>
					<th scope=\"col\">Edit</th>
					<th scope=\"col\">Delete</th>
				</tr>
			</thead>	
			<tfoot>
				<tr>
					<th scope=\"row\">Total</th>

					<td colspan=\"5\">$cnt Posts</td>
				</tr>
			</tfoot>
			<tbody>";		


		foreach ($query->result() as $row)
		{
			$out.=
			 "<tr>
			    <td><abbr title=\"$row->PostedDate\">".substr($row->PostedDate,0,10)."</abbr></td>
			    <td>".$row->Title."</td>
			    <td>".self::getstatus($row->Status)."</td>
			    <td>".anchor('blog/post/'.$row->URLTitle, 'View')."</td>
			    <td>".anchor("admin/EditBlog/".$row->PostID."/".$pageid,"Edit")."</td>
			    <td>".anchor("admin/DeleteBlog/".$row->PostID."/".$pageid."/".$row->URLTitle,"Delete")."</td>
			  </tr>";
			
			
		} 
		$out.= "</tbody></table>";
		$out.= $this->pagination->create_links();
		return $out;
		
	}
	  
	function RefinePagNav($page_id)
	{
			if($page_id==1)
			$page_id=0;
			
			return $page_id;
	}
	
	static function getstatus($status)
	{   
		$out="";
		switch($status)
		{
			case 1: $out="Published"; break;
			case 2: $out="Unpublished"; break;
			case 3: $out="Draft"; break;
			case 4: $out="Private"; break;
			case 5: $out="Trash"; break;
			default: $out="Unknown";
		}
		
		return $out;
	}			


}

